//
//  HttpServerRequest.cpp
//  myhttpserver
//
//  Created by tgy on 2018/3/19.
//  Copyright © 2018年 tgy. All rights reserved.
//

#include "HttpServerRequest.hpp"
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include <sstream>
#include "Utils.hpp"

#define READBUFSIZE 1024
//method 分发
void HttpServerRequest::dispatch(Client *client){
    
    
    switch (method) {
        case GET:
            
            getHander(client);
            break;
        case POST:
            postHander(client);
            break;
        case PUT:
            
            break;
        case DELETE:
            
            break;
        default:
            break;
    }
}

void HttpServerRequest::getHander(Client *client){
    
    std::string allPath = basePath + path ;
    struct stat st;
    
    int stat_resutl = stat(allPath.c_str(), &st);
    
    if (stat_resutl < 0) {  //没有这个文件或者目录
        

        return;
    }
    
//    st.st_mode
    if (S_ISDIR(st.st_mode)) {  //目录
        
        //读目录
        readDir(client, allPath);
    }else if (S_ISREG(st.st_mode)){  //文件
        //读文件
        readFile(client, allPath);
    }else{
        
        //错误的地址
        
    }
    
    
}


void HttpServerRequest::postHander(Client *client){
    
}


void HttpServerRequest::readFile(Client *client,std::string path){
    
    int fd = open(path.c_str(), O_RDONLY);
    
    if (fd < 0) {
        
        perror("open");
        
        return;
    }
    
    char buf[READBUFSIZE];
    

    std::string tmpRead;
    while (true) {
        
        bzero(buf, READBUFSIZE);
        ssize_t read_num = read(fd, buf, READBUFSIZE);
        if (read_num < 0) {
            
            perror("read_num");
            break;
        }else if (read_num == 0 ){
            //读完了
            break;
        }else{
            
            tmpRead.append(buf, read_num);
        }
    }
    
    if (!tmpRead.empty()) {
        
        SendMessageItem *smi = new SendMessageItem(tmpRead.c_str(),tmpRead.size());
        client->pushSendItem(smi);
    }
    
}
void HttpServerRequest::readDir(Client *client,std::string path){
    
    
    DIR *dir = opendir(path.c_str());
    //空文件夹
    if (dir == NULL) {
        
        
        return;
    }
    
    std::string sendData = "<!DOCTYPE html><html lang=\"zh\"><head><meta charset=\"UTF-8\"><title>Title</title></head><body>";
    std::ostringstream oss;
    do {
        
        struct dirent *dirt = readdir(dir);
        
        if (dirt == NULL) {
            
            break;
        }
        
        //文件名
       //    dirt->d_name;
        
        if (!Utils::canSendFileName(dirt->d_name)) {
            
            continue;
        }
        
        // //<a href="asd">asd</a><br>
        oss << "<a href=\"";
        if (this->path != "/") {
            
            oss << this->path;
        }
         oss<< "/" << dirt->d_name << "\">" << dirt->d_name << "</a><br>";
    } while (true);
    
    /*
     <!DOCTYPE html>
     <html lang="zh">
     <head>
     <meta charset="UTF-8">
     <title>Title</title>
     </head>
     <body>
     <a href="asd">asd</a><br>
     <a href="asd">asd</a>
     </body>
     </html>
     */
    sendData.append(oss.str()).append("</body></html>");
    SendMessageItem *smi = new SendMessageItem(sendData.c_str(),sendData.size());
    client->pushSendItem(smi);
}


